const { bodyanalysis, speech, face } = require('baidu-aip-sdk')
import path from 'path'
import { exec } from 'child_process'

const APP_ID = "16400774";
const API_KEY = "CeOuAOvi7QZWPMXdjQWbPuP3";
const SECRET_KEY = "gPLS7F91SB7AGIlQSunqufX70oKRiASm";

export const getface = async (image: string): Promise<{ face_num?: number, face_list?: { [key: string]: any } }> => {
    const client = new face(APP_ID, API_KEY, SECRET_KEY)
    const { error_code, result } = await client.detect(image, 'BASE64', { timeout: 2500 })
    if (error_code === 0) {
        return result
    }
    return {}
}



export const gesture = async (image: string): Promise<any> => {
    const client = new bodyanalysis(APP_ID, API_KEY, SECRET_KEY)

    return client.gesture(image, { timeout: 2500 })
}

export const recognize = async (name: Buffer) => {
    const client = new speech(APP_ID, API_KEY, SECRET_KEY)
    const buffer = Buffer.from(name)
    return client.recognize(buffer, 'pcm', 16000, { dev_pid: '1537' })
}

export function ffmpeg(filepath: string): Promise<string> {
    return new Promise((res, rej) => {
        const d = `${path.join(filepath)}`;
        console.log(`ffmpeg -y -f s16le -ac 1 44100 -i  ${filepath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 ${d}`)
        exec(`ffmpeg -y -f s16le -ac 1 44100 -i  ${filepath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 ${d}`, (err, _stdout, _stderr) => {
            if (err) {
                rej(err)
            }
            res(d)
        });
    })
}


